package com.wang.controller;

import com.wang.Param.UserRegisterParam;
import com.wang.commons.exception.ParamException;
import com.wang.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@Slf4j
@Api(tags = "用户注册")
public class RegisterController {

    @ApiOperation("使用手机号/邮箱注册接口")
    @PostMapping("/userPhoneRegister")
    public Result userPhoneLogin(@ApiParam(name = "userLoginPhoneParam", value = "注册参数", required = true) @Valid @RequestBody UserRegisterParam userRegisterParam, BindingResult bindingResult){
        log.info("进行参数校验：{}", userRegisterParam);
        if(bindingResult.hasErrors()){
            throw new ParamException();
        }
        log.info("userRegisterParam:{}", userRegisterParam);
        if(ObjectUtils.isEmpty(userRegisterParam)){
            log.info("注册参数不为空:{}", userRegisterParam);
            if(userRegisterParam.getUserTelOrEmail().endsWith("com")&&userRegisterParam.getUserTelOrEmail().contains("@")){
                log.info("执行邮箱登录:{}", userRegisterParam);
                return null;
            }else{
                log.info("执行账号登录:{}", userRegisterParam);
                return null;
            }
        }
        log.info("执行登录失败");
        return null;
    }
}
